package com.wf.utils;


import org.apache.poi.POIXMLProperties;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReadWordContent {

    public static void main(String[] args) throws Exception {
        String filePath = "template/contractTemplate.docx";
        ReadWordContent readWordContent = new ReadWordContent();
//        readWordContent.readByExtractor(filePath);
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("wangwu");
        list.add("wwwwwwww");
        list.add("fffffffff");
        readWordContent.writeDataToWord(filePath,list);
    }

    /**
     * 通过XWPFWordExtractor访问XWPFDocument的内容
     * @throws Exception
     */
    public void readByExtractor(String path) throws Exception {
        ClassPathResource resource = new ClassPathResource(path);
        InputStream stream = resource.getInputStream();
        XWPFDocument doc = new XWPFDocument(stream);
        XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
        String text = extractor.getText();
        System.out.println(text);
        /**
         * 打印文档信息
         */
        POIXMLProperties.CoreProperties coreProps = extractor.getCoreProperties();
        this.printCoreProperties(coreProps);
        this.close(stream);
    }

    /**
     * 输出CoreProperties信息
     * @param coreProps
     */
    private void printCoreProperties(POIXMLProperties.CoreProperties coreProps) {
        String category = coreProps.getCategory(); //分类
        String creator = coreProps.getCreator(); //创建者,Microsoft Office User
        Date createdDate = coreProps.getCreated(); //创建时间
        String title = coreProps.getTitle(); //标题
        String description = coreProps.getDescription(); //描述，默认为null

        System.out.println(category);
        System.out.println(creator);
        System.out.println(createdDate);
        System.out.println(title);
        System.out.println(description);
    }

    /**
     * 关闭输入流
     * @param is
     */
    private void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void writeDataToWord(String fileName, List<String> dataListArg) throws IOException {
        ClassPathResource resource = new ClassPathResource(fileName);
        InputStream stream = resource.getInputStream();
        XWPFDocument doc = new XWPFDocument(stream);
        FileOutputStream outputStream = new FileOutputStream("E://AAA.docx");
        // 遍历dataListArg，并将每项内容添加到文档中
        for (String data : dataListArg) {
            // 创建一个段落
            XWPFParagraph paragraph = doc.createParagraph();
            // 在段落中添加文本
            paragraph.createRun().setText(data);
        }

        doc.write(outputStream);
        doc.close();
        outputStream.close();
        WordUtils.convertToPDF("E://AAA.docx", "E://AAA.pdf");

    }

}